﻿using System.Collections.Generic;
using System.Linq;
using Zeus.Interop.SampleSite.Models;

namespace Zeus.Interop.SampleSite.Stubs
{
    public class CustomerService : ICustomerService
    {
        private readonly IRepository _repository;

        public CustomerService(IRepository repository)
        {
            _repository = repository;
        }

        public CustomerService() : this(new StaticData())
        {
            
        }

        public IEnumerable<Customer> GetCustomers()
        {
            return _repository.Customers;
        }

        public Customer GetCustomerById(int id)
        {
            return _repository.Customers.SingleOrDefault(customer => customer.Id == id);
        }

        public IEnumerable<Order> GetOrdersForCustomer(int customerId)
        {
            var orders = _repository.Orders.Where(order => order.CustomerId == customerId);
            foreach (var order in orders)
                foreach (var item in order.Items)
                {
                    var item1 = item;
                    item.Product = _repository.Products.SingleOrDefault(product => product.Id == item1.ProductId);
                }

            return orders;
        }
    }
}